<?php

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

error_reporting( E_ALL );
//error_reporting( E_ERROR | E_WARNING | E_PARSE );

define( 'SUNCMS', str_replace( 'include', '', dirname( strtr(__FILE__, '\\', '/') ) ) );

define( 'TIME_START', microtime() );

define( 'TIME', time() );

define( 'MAGIC_QUOTES_GPC', get_magic_quotes_gpc() );

set_magic_quotes_runtime( 0 );

unset( $LANG, $HTTP_ENV_VARS, $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_POST_FILES, $HTTP_COOKIE_VARS );

!file_exists( SUNCMS.'datas/install.lock' ) && header( "Refresh:0; url='install/index.php'" );

require( SUNCMS.'include/config/config.inc.php' );
require( SUNCMS.'include/global.func.php' );
require( SUNCMS.'include/file_dir.func.php' );
require( SUNCMS.'include/template.func.php' );
require( SUNCMS.'include/cache.func.php' );
require( SUNCMS.'include/librarys/mysql.class.php' );
require( SUNCMS.'include/librarys/pagination.class.php' );

$db = new Mysql();

define( 'IP', get_ip() );

define( 'HTTP_REFERER', isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' );

define( 'SCHEME', $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://' );

define( 'SITE_URL', SCHEME.$_SERVER['HTTP_HOST'].'/'.CMS_PATH );

define( 'SCRIPT_NAME', get_fileext( preg_replace("/(.*)\.php(.*)/i", "\\1.php", $_SERVER['PHP_SELF'] ) , '/' ) );

define( 'CURRENT_URL', SCHEME.$_SERVER['HTTP_HOST'].'/'.CMS_PATH.SCRIPT_NAME.( $_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : '' ) );

define( 'SITE_PATH', str_replace( REL_PATH, '', SUNCMS ) );

function_exists( 'date_default_timezone_set' ) && date_default_timezone_set( TIMEZONE );

header( 'content-type:text/html; charset='.CHARSET );

$cache = cache_read( 'common' );
if( !$cache )
{
	cache_all();
	$cache = cache_read( 'common' );
}
@extract( $cache );
unset( $cache );

//gzip
my_ob_start();

if( !MAGIC_QUOTES_GPC )
{
	$_POST = new_addslashes( $_POST );
	$_GET = new_addslashes( $_GET );
	$_COOKIE = new_addslashes( $_COOKIE );
}

$SUNCMS['site_close'] && show_error( $SUNCMS['site_colse_why'] );

ban_ip( IP ) && show_error( '对不起，您的IP已被管理员禁止访问！' );

if( !defined( 'SUN_ADMIN' ) && !in_array( SCRIPT_NAME, array( 'editor.php', 'authcode.php' ) ) )
{
	//防非法信息
	if( FILTER_ENABLE && filter_word() ) show_msg( '您所提交的内容中包含了非法信息： ' . ILLEGAL_WORD.' .' );
	//防刷新
    if( $SUNCMS['limit_refresh_time'] )
	{
		require( SUNCMS.'include/librarys/times.class.php' );
		$cc = new Times();
		$cc->set( 'cc', $SUNCMS['limit_refresh_time'], 1 );
		if( $cc->check() ) show_msg( '请不要在 '.$SUNCMS['limit_refresh_time'].' 秒内重复刷新页面！' );
		$cc->add();
		unset( $cc );
	}
}

//自动登录
$user_id = 0; $username = ''; $group_id = 3;
$slogin = get_cookie( 'slogin' );
if( $slogin )
{
	$user_id = intval( $slogin['user_id'] );
	$password = $slogin['password'];
	$member_info = $db->get_one( DB_PRE.'member', array( 'id' => $user_id ) );
	if( $member_info['password'] === $password )
	{
		if( $member_info['group_id'] == 2 )
		{
			unset_cookie( 'slogin' );
			show_msg( '您的帐号已经被管理员禁止访问！' );
			exit;
		}
		@extract( $member_info );
		$user_id = $id;
	}
	else
	{
		$user_id = 0; $username = ''; $group_id = 3;
		unset_cookie( 'slogin' );
	}
	unset( $member_info, $password, $id );
}
$group_priv = cache_read( 'member_group_'.$group_id );